﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam
{
    public partial class FormPhanLoai : Form
    {
        NhanVien nv;
        string action;
        CXulyRole XulyRole;

        public FormPhanLoai(NhanVien n)
        {
            InitializeComponent();
            nv = n;
        }

        private void FormPhanLoai_Load(object sender, EventArgs e)
        {
            XulyRole = new CXulyRole();
            var kq = XulyRole.GetListRole().Where(x => x.RoleStatus == "live");
            if (kq.Count() > 0)
            {
                HienThiDsRole(kq.ToList<Role>());
            }
        }

        private void btnphanquyen_Click(object sender, EventArgs e)
        {
            FormPhanQuyen f = new FormPhanQuyen();
            f.ShowDialog();

            if (f.DsQuyen.Count > 0)
            {
                dgvphanloai_quyen.Rows.Clear();
                foreach (Quyen q in f.DsQuyen)
                {
                    int i = dgvphanloai_quyen.Rows.Add();
                    dgvphanloai_quyen.Rows[i].Cells[0].Value = q.QuyenID;
                    dgvphanloai_quyen.Rows[i].Cells[1].Value = q.QuyenNo;
                    dgvphanloai_quyen.Rows[i].Cells[2].Value = q.QuyenStatus;
                }
            }
        }

        private void btntim_Click(object sender, EventArgs e)
        {
            if (txttimtheotenrole.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập tên vào ô tên vị trí");
                return;
            }

            var kq = XulyRole.GetListRole().Where(x=>x.RoleName.Contains(txttimtheotenrole.Text));
            if (kq.Count() > 0)
                HienThiDsRole(kq.ToList<Role>());
        }

        private void btnTao_Click(object sender, EventArgs e)
        {
            #region Xử lý giao diện
            //Xử lý các nút
            btnTao.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnSave.Enabled = true;
            btnHuy.Enabled = true;
            btnphanquyen.Visible = true;

            txtten.Text = "";
            dgvphanloai_quyen.Rows.Clear();

            txtma.Text = (XulyRole.GetListRole().Count + 1).ToString();
            #endregion

            #region Xử lý dữ liệu
            action = "add";
            #endregion
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            #region Xử lý giao diện
            //Xử lý các nút
            btnTao.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnSave.Enabled = true;
            btnHuy.Enabled = true;
            btnphanquyen.Visible = true;
            #endregion

            #region Xử lý dữ liệu
            action = "edit";
            #endregion
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            #region Xử lý giao diện
            //Xử lý các nút
            btnTao.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnSave.Enabled = true;
            btnHuy.Enabled = true;

            
            #endregion

            #region Xử lý dữ liệu
            action = "delete";
            #endregion
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            #region Xử lý giao diện
            //Xử lý các nút
            btnTao.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
            btnSave.Enabled = false;
            btnHuy.Enabled = false;
            btnphanquyen.Visible = false;
            #endregion

            #region Xử lý dữ liệu
            switch (action)
            {
                case ("add"):
                    {
                        Role r = new Role();
                        r.RoleID = int.Parse(txtma.Text);
                        r.RoleName = txtten.Text;
                        r.RoleStatus = "live";
                        foreach (DataGridViewRow dr in dgvphanloai_quyen.Rows)
                        {
                            if (dr.IsNewRow)
                                continue;
                            PhanQuyen pq = new PhanQuyen();
                            pq.FK_QuyenID = int.Parse(dr.Cells[0].Value.ToString());
                            r.PhanQuyens.Add(pq);
                        }

                        XulyRole.ThemRole(r);
                        break;
                    }
                case ("edit"):
                    {
                        Role r = new Role();
                        r.RoleID = int.Parse(txtma.Text);
                        r.RoleName = txtten.Text;
                        foreach (DataGridViewRow dr in dgvphanloai_quyen.Rows)
                        {
                            if (dr.IsNewRow)
                                continue;
                            PhanQuyen pq = new PhanQuyen();
                            pq.FK_QuyenID = int.Parse(dr.Cells[0].Value.ToString());
                            r.PhanQuyens.Add(pq);
                        }

                        XulyRole.SuaRole(r);
                        break;
                    }
                case("delete"):
                    {
                        XulyRole.XoaRole(int.Parse(txtma.Text));
                        txtma.Text = "";
                        txtten.Text = "";
                        dgvphanloai_quyen.Rows.Clear();
                        break;
                    }
                default:
                    break;
            }
            #endregion
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            #region Xử lý giao diện
            //Xử lý các nút
            btnTao.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
            btnSave.Enabled = false;
            btnHuy.Enabled = false;
            btnphanquyen.Visible = false;
            if (action == "add")
            {
                txtma.Text = "";
                txtten.Text = "";
                dgvphanloai_quyen.Rows.Clear();
            }
            #endregion
        }

        private void lstPhanLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            Role r = XulyRole.TimRole(int.Parse(lstPhanLoai.SelectedItems[0].Text.ToString()));
            HienThiRole(r);
            tabControlPhanLoai.SelectedIndex = 1;
        }

        private void HienThiDsRole(List<Role> ds)
        {
            lstPhanLoai.Items.Clear();

            foreach (Role r in ds)
            {
                ListViewItem lvi = lstPhanLoai.Items.Add(r.RoleID.ToString());
                lvi.SubItems.Add(r.RoleName);
                lvi.SubItems.Add(r.RoleDescription);
            }
        }

        private void HienThiRole(Role r)
        {
            txtma.Text = r.RoleID.ToString();
            txtten.Text = r.RoleName;
            HienThiPhanQuyen(r.PhanQuyens.ToList<PhanQuyen>());
        }

        private void HienThiPhanQuyen(List<PhanQuyen> dspq)
        {
            dgvphanloai_quyen.Rows.Clear();
            foreach (PhanQuyen pq in dspq)
            {
                int i = dgvphanloai_quyen.Rows.Add();
                dgvphanloai_quyen.Rows[i].Cells[0].Value = pq.FK_QuyenID;
                dgvphanloai_quyen.Rows[i].Cells[1].Value = pq.Quyen.QuyenNo;
                dgvphanloai_quyen.Rows[i].Cells[2].Value = pq.Quyen.QuyenDescription;
            }
        }
    }
}
